/*
 * View by Point list
 */
package com.trucle.vntraffic;

import java.util.ArrayList;
import java.util.HashMap;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.util.Log;

public class ListviewTraffic extends Activity {

	 private static String url = "http://q.esolvn.com/traffic/?action=mobile&java=1";
	 
	 // JSON Node names
	 static final String TAG_TITLE = "title";
	 static final String TAG_LAT = "lat";
	 static final String TAG_LONG = "long";
	 static final String TAG_DISTRICT = "folder_id";
	 static final String TAG_STATUS = "last_value";
	 static final String TAG_THUMB_URL = "thumb";
	 
	 private static final String TAG_ID = "id";
	 private static final String TAG_NAME = "name";
	   	    
	 ListView list;
	 LazyAdapter adapter;
	 
	 // listPoint JSONArray
	 JSONArray point_dist = null;
	 JSONArray point_trap = null;
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.district);
        
        //Hashmap for ListView
        ArrayList<HashMap<String, String>> pointList = new ArrayList<HashMap<String, String>>();
        ArrayList<HashMap<String, String>> distList = new ArrayList<HashMap<String, String>>();
 
        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();
 
        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url,1);
        HashMap<String, String> distL = new HashMap<String, String>();
        
        try {
        	point_trap = json.getJSONArray("subcat");
        	Log.v("trucle", "the number of trap = "+point_trap.length());
        	for(int i = 0; i < point_trap.length(); i++){
                JSONObject c = point_trap.getJSONObject(i);
 
                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
 
                // adding each child node to HashMap key => value
                distL.put(id,name);
                
        	}
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
        
        try {
            // Getting Array of Contacts
        	point_dist = json.getJSONArray("channel");
        	
            // looping through All Contacts
            for(int i = 0; i < point_dist.length(); i++){
                JSONObject c = point_dist.getJSONObject(i);
 
                // Storing each json item in variable
                String id = c.getString("id");
                String title = c.getString(TAG_TITLE);
                String plat = c.getString(TAG_LAT);
                String plong = c.getString(TAG_LONG);
                String districtT = c.getString(TAG_DISTRICT);
                String district = null;
                
                for (HashMap.Entry<String,String> entry : distL.entrySet()) {
                	if (entry.getKey().equals(districtT))
                	{
                		district = entry.getValue();
                		Log.v("trucle","Key = "+entry.getKey()+"  value = "+district);
                	}
                }
                
                String status = c.getString(TAG_STATUS);
                String thumb_url = null;
                
                if (status.equals("1"))
                {
                	thumb_url = "http://q.esolvn.com/traffic/images/b1.png";
                }
                else if (status.equals("2"))
                {
                	thumb_url = "http://q.esolvn.com/traffic/images/b2.png";
                }
 
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
 
                // adding each child node to HashMap key => value
                map.put(TAG_TITLE, title);
                map.put(TAG_LAT, plat);
                map.put(TAG_LONG, plong);
                map.put(TAG_DISTRICT,district);
                map.put(TAG_STATUS, status);
                map.put(TAG_THUMB_URL,thumb_url);
 
                // adding HashList to ArrayList
                pointList.add(map);
                
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        //Updating parsed JSON data into ListView
        
        list=(ListView)findViewById(R.id.list);
		
		// Getting adapter by passing json data ArrayList
        adapter=new LazyAdapter(this, pointList);        
        list.setAdapter(adapter);
        

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
							

			}
		});		
        Button btnShowMap = (Button) findViewById(R.id.btn_show_map);
        btnShowMap.setOnClickListener(new View.OnClickListener() {
			public void onClick(View arg0) {
				Intent i = new Intent(getApplicationContext(),
						VnTraffic.class);
				// staring activity
				startActivity(i);
			}
		});

    }

    
}
